package br.ufpe.cin.camid.core.lifecycle;

import java.util.Calendar;

import br.ufpe.cin.camid.core.objects.ManagedObjectType;

public class ManagedObjectLease 
{
	private final String objectId;
	
	private final ManagedObjectType type;
	
	private final Long lastRenewalTimestamp;
	
	private final Long deactivationTime;
	
	public ManagedObjectLease(final String objectId, final ManagedObjectType type, final Long deactivationTime) {
		super();
		this.objectId = objectId;
		this.type = type;
		this.deactivationTime = deactivationTime;
		this.lastRenewalTimestamp = Calendar.getInstance().getTimeInMillis();
	}

	public Long getDeactivationTime() {
		return deactivationTime;
	}
	
	public String getObjectId() {
		return objectId;
	}
	
	public ManagedObjectType getType() {
		return type;
	}

	public boolean hasActivationExpired()
	{
		boolean expired = false;
		
		Long currentTimestamp = Calendar.getInstance().getTimeInMillis();
		Long timestampDelta = currentTimestamp - lastRenewalTimestamp;

		if(timestampDelta > deactivationTime)
		{
			expired = true;
		}
		return expired;
	}
	
	public ManagedObjectLease renewLease()
	{
		ManagedObjectLease newLease = new ManagedObjectLease(this.objectId, this.type, this.deactivationTime);
		return newLease;
	}
	
	
	
}
